package com.fufufu.mail.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.fufufu.common.valid.AddGroup;
import com.fufufu.mail.product.entity.AttrEntity;
import com.fufufu.mail.product.service.AttrAttrgroupRelationService;
import com.fufufu.mail.product.service.AttrService;
import com.fufufu.mail.product.service.CategoryService;
import com.fufufu.mail.product.vo.AttrGroupAttrRelationVo;
import com.fufufu.mail.product.vo.AttrGroupWithBaseAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.fufufu.mail.product.entity.AttrGroupEntity;
import com.fufufu.mail.product.service.AttrGroupService;
import com.fufufu.common.utils.PageUtils;
import com.fufufu.common.utils.R;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * 属性分组
 *
 * @author zyx
 * @email 1262074394@qq.com
 * @date 2022-09-15 08:15:48
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private AttrService attrService;

    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @GetMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        if (attrGroup == null) {
            return R.error();
        }
        attrGroup.setCatelogPath(categoryService.findCatelogPath(attrGroup.getCatelogId()));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Validated(AddGroup.class) @RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 通过属性分组id查询到其对应的基本属性
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getAttrById(@PathVariable("attrgroupId") Long attrgroupId) {

        List<AttrEntity> data = attrService.getAttrByAttrgroupId(attrgroupId);

        return R.ok().put("data", data);
    }

    /**
     * 通过属性分组id查询到其未关联的属性
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoAttrById(@PathVariable("attrgroupId") Long attrgroupId,
                           @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoAttrByAttrgroupId(attrgroupId, params);
        return R.ok().put("page", page);
    }

    /**
     * 通过属性分组删除基本属性
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrGroupAttrRelationVo> relations) {

        attrGroupService.deleteRelation(relations);

        return R.ok();
    }

    /**
     * 新建属性分组和基本属性的关联关系
     */
    @PostMapping("/attr/relation")
    public R relation(@RequestBody List<AttrGroupAttrRelationVo> relations) {

        attrAttrgroupRelationService.saveRelations(relations);

        return R.ok();
    }


    /**
     * 获取分类下所有属性分组，以及关联属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttr(@PathVariable("catelogId") Long catelogId) {
        // 通过类别id可以获取到其所拥有的属性组,在当前的service实现
        List<AttrGroupWithBaseAttrVo> data = attrGroupService.getGroupsByCatelogId(catelogId);
        // 通过属性组id获取其对应的属性,在attrservice实现通过（这里可以用循环查询实现，但是不推荐在循环中查数据库
        // ，好像不太好啊，如果一次性查询完的话，又要进行匹配）
        return R.ok().put("data", data);

    }
}
